package com.wcfsy.wechat.wx.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wcfsy.wechat.wx.entity.ProblemInfo;
import com.wcfsy.wechat.wx.pagination.IPage;
import com.wcfsy.wechat.wx.pagination.impl.Page;
import com.wcfsy.wechat.wx.sys.dao.ProblemInfoDao;
import com.wcfsy.wechat.wx.util.JsonMessage;

@Controller
@RequestMapping("/cms/problem/")
public class ProblemController {

	@Resource
	private ProblemInfoDao problemInfoDao;

	@RequestMapping("view/page")
	public String page() {
		return "problem/problem_list";
	}

	@RequestMapping("view")
	@ResponseBody
	public IPage<ProblemInfo> view(ProblemInfo info, int page, int rows) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("index", page);
		paramMap.put("size", rows);
		paramMap.put("limit", "limit");
		return new Page<ProblemInfo>(problemInfoDao.findByParam(paramMap),
				problemInfoDao.count(paramMap), page, rows);
	}

	@RequestMapping("add/page")
	public String preAdd() {
		return "problem/problem_add";
	}

	@RequestMapping("add")
	@ResponseBody
	public JsonMessage add(ProblemInfo info) {
		try {
			problemInfoDao.add(info);
			return JsonMessage.getSuccessJson("新增成功");
		} catch (Exception e) {
			return JsonMessage.getErrorJson("添加常见问题失败");
		}
	}

	@RequestMapping("modify/page")
	public ModelAndView preModify(int id) {
		ModelAndView mav = new ModelAndView("problem/problem_modify");
		mav.addObject("data", problemInfoDao.getById(id));
		return mav;
	}

	@RequestMapping("modify")
	@ResponseBody
	public JsonMessage modify(ProblemInfo info) {
		try {
			info.setUpdateDate(new Date());
			problemInfoDao.update(info);
			return JsonMessage.getSuccessJson("修改成功");
		} catch (Exception e) {
			return JsonMessage.getErrorJson("修改失败");
		}
	}

	@RequestMapping("delete")
	@ResponseBody
	public JsonMessage delete(int id) {
		try {
			problemInfoDao.deleteById(id);
			return JsonMessage.getSuccessJson("删除成功");
		} catch (Exception e) {
			return JsonMessage.getErrorJson("删除失败");
		}
	}
}
